package qianfeng.com.meijiadaka.adapter;

import android.content.Context;
import android.content.Intent;
import android.support.v7.widget.GridLayoutManager;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;
import android.widget.Toast;

import com.daimajia.slider.library.SliderLayout;
import com.daimajia.slider.library.SliderTypes.BaseSliderView;
import com.daimajia.slider.library.SliderTypes.DefaultSliderView;
import com.squareup.picasso.Picasso;

import java.util.ArrayList;
import java.util.List;

import qianfeng.com.meijiadaka.CommodityDtailsActivity;
import qianfeng.com.meijiadaka.R;
import qianfeng.com.meijiadaka.SpeciaDetailsActivity;
import qianfeng.com.meijiadaka.bean.Bean;
import qianfeng.com.meijiadaka.utils.RecyclerViewDivider;


/**
 * Created by Think on 2017/2/7.
 */

public class ShopAdapter extends BaseAdapter {
    private List<Object> objects;
    private Context context;
    private Bean mBean;

    List<Bean.ResultBean.SpecialTopicBean> st;
    List<Bean.ResultBean.BrandAreaBean> bab;
    List<Bean.ResultBean.BestsellingAreaBean> bla;
    List<Bean.ResultBean.MiddleAdsBean> ma;


    public ShopAdapter(Context context, Bean mBean) {
        this.context = context;
        this.mBean = mBean;

        objects = new ArrayList<>();
        objects.add(mBean.getResult().getSaleCommodities());  //限时促销
        objects.add(mBean.getResult().getSpecialTopic()); //美咖专题
        objects.add(mBean.getResult().getBrandArea()); //品牌推荐
        objects.add(mBean.getResult().getMiddleAds());  //轮播
        objects.add(mBean.getResult().getBestsellingArea()); //热卖，前三个
        objects.add(mBean.getResult().getRecommendedArea()); // 商品推荐
        Log.e("ShopAdapter:   objects==", objects.size() + "");
    }

    @Override
    public int getItemViewType(int position) {
        st = mBean.getResult().getSpecialTopic();
        bab = mBean.getResult().getBrandArea();
        bla = mBean.getResult().getBestsellingArea();
        ma = mBean.getResult().getMiddleAds();

        if (objects.get(position) instanceof Bean.ResultBean.SaleCommoditiesBean) {
            return 0;
        } else if (objects.get(position).equals(st)) {
            return 1;
        } else if (objects.get(position).equals(bab)) {
            return 2;
        } else if (objects.get(position).equals(bla)) {
            return 3;
        } else if (objects.get(position).equals(ma)) {
            return 5;
        } else {
            return 4;
        }
    }

    @Override
    public int getCount() {
        return objects.size();
    }

    @Override
    public Object getItem(int position) {
        return objects.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public int getViewTypeCount() {
        return 6;
    }

    @Override
    public View getView(final int position, View convertView, ViewGroup parent) {
        ViewHolder viewHolder = null;
        ViewHolder1 viewHolder1 = null;
        ViewHolder2 viewHolder2 = null;
        ViewHolder3 viewHolder3 = null;
        ViewHolder4 viewHolder4 = null;
        ViewHolder5 viewHolder5 = null;

        int type = getItemViewType(position);

        if (convertView == null) {
            switch (type) {
                case 0:
                    convertView = LayoutInflater.from(context).inflate(R.layout.shop_timed_promotions, parent, false);
                    viewHolder = new ViewHolder();
                    viewHolder.rl = (RelativeLayout) convertView.findViewById(R.id.shop_timed_layout_rl);
                    viewHolder.rv = (RecyclerView) convertView.findViewById(R.id.shop_timed_recyclerView);
                    convertView.setTag(viewHolder);
                    break;
                case 1:
                    convertView = LayoutInflater.from(context).inflate(R.layout.shop_special, parent, false);
                    viewHolder1 = new ViewHolder1();
                    viewHolder1.rv = (RecyclerView) convertView.findViewById(R.id.shop_special_recyclerview);
                    convertView.setTag(viewHolder1);
                    break;
                case 2:
                    convertView = LayoutInflater.from(context).inflate(R.layout.shop_brand_recommendation, parent, false);
                    viewHolder2 = new ViewHolder2();
                    viewHolder2.tv1 = (TextView) convertView.findViewById(R.id.brand_recommend_tv1);
                    viewHolder2.tv11 = (TextView) convertView.findViewById(R.id.brand_recommend_tv11);
                    viewHolder2.tv2 = (TextView) convertView.findViewById(R.id.brand_recommend_tv2);
                    viewHolder2.tv22 = (TextView) convertView.findViewById(R.id.brand_recommend_tv22);
                    viewHolder2.tv3 = (TextView) convertView.findViewById(R.id.brand_recommend_tv3);
                    viewHolder2.tv33 = (TextView) convertView.findViewById(R.id.brand_recommend_tv33);
                    viewHolder2.tv4 = (TextView) convertView.findViewById(R.id.brand_recommend_tv4);
                    viewHolder2.tv44 = (TextView) convertView.findViewById(R.id.brand_recommend_tv44);
                    viewHolder2.tv5 = (TextView) convertView.findViewById(R.id.brand_recommend_tv5);
                    viewHolder2.tv55 = (TextView) convertView.findViewById(R.id.brand_recommend_tv55);
                    viewHolder2.iv1 = (ImageView) convertView.findViewById(R.id.brand_recommend_iv1);
                    viewHolder2.iv2 = (ImageView) convertView.findViewById(R.id.brand_recommend_iv2);
                    viewHolder2.iv3 = (ImageView) convertView.findViewById(R.id.brand_recommend_iv3);
                    viewHolder2.iv4 = (ImageView) convertView.findViewById(R.id.brand_recommend_iv4);
                    viewHolder2.iv5 = (ImageView) convertView.findViewById(R.id.brand_recommend_iv5);
                    convertView.setTag(viewHolder2);
                    break;
                case 3:
                    convertView = LayoutInflater.from(context).inflate(R.layout.shop_hot_sell, parent, false);
                    viewHolder3 = new ViewHolder3();
                    viewHolder3.tv1 = (TextView) convertView.findViewById(R.id.hot_sell_tv1);
                    viewHolder3.tv11 = (TextView) convertView.findViewById(R.id.hot_sell_tv11);
                    viewHolder3.tv2 = (TextView) convertView.findViewById(R.id.hot_sell_tv2);
                    viewHolder3.tv22 = (TextView) convertView.findViewById(R.id.hot_sell_tv22);
                    viewHolder3.tv3 = (TextView) convertView.findViewById(R.id.hot_sell_tv3);
                    viewHolder3.tv33 = (TextView) convertView.findViewById(R.id.hot_sell_tv33);
                    viewHolder3.iv1 = (ImageView) convertView.findViewById(R.id.hot_sell_iv1);
                    viewHolder3.iv2 = (ImageView) convertView.findViewById(R.id.hot_sell_iv2);
                    viewHolder3.iv3 = (ImageView) convertView.findViewById(R.id.hot_sell_iv3);
                    convertView.setTag(viewHolder3);
                    break;
                case 4:
                    convertView = LayoutInflater.from(context).inflate(R.layout.shop_commodity_recommend, parent, false);
                    viewHolder4 = new ViewHolder4();
                    viewHolder4.rv = (RecyclerView) convertView.findViewById(R.id.shop_commodity_recyclerview);
                    convertView.setTag(viewHolder4);
                    break;
                case 5:
                    convertView = LayoutInflater.from(context).inflate(R.layout.shop_lunbo, parent, false);
                    viewHolder5 = new ViewHolder5();
                    viewHolder5.slider = (SliderLayout) convertView.findViewById(R.id.slider2);
                    convertView.setTag(viewHolder5);
                    break;

            }

        } else {
            switch (type) {
                case 0:
                    viewHolder = (ViewHolder) convertView.getTag();
                    break;
                case 1:
                    viewHolder1 = (ViewHolder1) convertView.getTag();
                    break;
                case 2:
                    viewHolder2 = (ViewHolder2) convertView.getTag();
                    break;
                case 3:
                    viewHolder3 = (ViewHolder3) convertView.getTag();
                    break;
                case 4:
                    viewHolder4 = (ViewHolder4) convertView.getTag();
                    break;
                case 5:
                    viewHolder5 = (ViewHolder5) convertView.getTag();
                    break;
            }
        }


        //赋值
        switch (type) {
            case 0:

                if (objects.get(position) instanceof Bean.ResultBean.SaleCommoditiesBean) {
//                    List<Bean.ResultBean.SaleCommoditiesBean.HeadCommoditiesBean> commodities =
//                        ((Bean.ResultBean.SaleCommoditiesBean) objects.get(position)).getCommodities();
                    viewHolder.rl.setOnClickListener(new View.OnClickListener() {
                        @Override
                        public void onClick(View v) {
                            Toast.makeText(context, "跳转", Toast.LENGTH_SHORT).show();
                        }
                    });
                    ShopTimedPromotionsRecyclerAdapter adapter = new ShopTimedPromotionsRecyclerAdapter(mBean, context);
                    LinearLayoutManager manager = new LinearLayoutManager(context, LinearLayoutManager.HORIZONTAL, false);
                    viewHolder.rv.setLayoutManager(manager);
                    adapter.setOnItemClickListener(new ShopTimedPromotionsRecyclerAdapter.OnItemClickListener() {
                        @Override
                        public void onClick(int index) {
                            Toast.makeText(context, "点击了" + index, Toast.LENGTH_SHORT).show();

                            Intent intent = new Intent(context, CommodityDtailsActivity.class);
                            intent.putExtra("commodityId", mBean.getResult().getSaleCommodities().getCommodities().get(index).getCommodityId());
                            context.startActivity(intent);
                        }
                    });
                    viewHolder.rv.setAdapter(adapter);
                }
                break;
            case 1:

                if (objects.get(position).equals(st)) {
                    ShopSpeciaRecyclerAdapter adapter = new ShopSpeciaRecyclerAdapter(mBean, context);
                    LinearLayoutManager manager = new LinearLayoutManager(context, LinearLayoutManager.HORIZONTAL, false);
                    viewHolder1.rv.setLayoutManager(manager);
                    viewHolder1.rv.addItemDecoration(new RecyclerViewDivider(context, LinearLayoutManager.VERTICAL));
                    adapter.setOnItemClickListener(new ShopSpeciaRecyclerAdapter.OnItemClickListener() {
                        @Override
                        public void onClick(int index) {
                            Intent intent = new Intent(context, SpeciaDetailsActivity.class);
                            intent.putExtra("id", st.get(index).getTopicId());
                            context.startActivity(intent);
                        }
                    });
                    viewHolder1.rv.setAdapter(adapter);
                }
                break;
            case 2:
                if (objects.get(position).equals(bab)) {
                    viewHolder2.tv1.setText(bab.get(0).getCommodityName() + "");
                    viewHolder2.tv11.setText(bab.get(0).getMinDiscount() + "");
                    viewHolder2.tv2.setText(bab.get(1).getCommodityName() + "");
                    viewHolder2.tv22.setText(bab.get(1).getMinDiscount() + "");
                    viewHolder2.tv3.setText(bab.get(2).getCommodityName() + "");
                    viewHolder2.tv33.setText(bab.get(2).getMinDiscount() + "");
                    viewHolder2.tv4.setText(bab.get(3).getCommodityName() + "");
                    viewHolder2.tv44.setText(bab.get(3).getMinDiscount() + "");
                    viewHolder2.tv5.setText(bab.get(4).getCommodityName() + "");
                    viewHolder2.tv55.setText(bab.get(4).getMinDiscount() + "");
                    Picasso.with(context).load(bab.get(0).getCommodityIcon()).placeholder(R.mipmap.ic_default_image).into(viewHolder2.iv1);
                    Picasso.with(context).load(bab.get(1).getCommodityIcon()).placeholder(R.mipmap.ic_default_image).into(viewHolder2.iv2);
                    Picasso.with(context).load(bab.get(2).getCommodityIcon()).placeholder(R.mipmap.ic_default_image).into(viewHolder2.iv3);
                    Picasso.with(context).load(bab.get(3).getCommodityIcon()).placeholder(R.mipmap.ic_default_image).into(viewHolder2.iv4);
                    Picasso.with(context).load(bab.get(4).getCommodityIcon()).placeholder(R.mipmap.ic_default_image).into(viewHolder2.iv5);
                    final Intent intent = new Intent(context, CommodityDtailsActivity.class);
                    viewHolder2.iv1.setOnClickListener(new View.OnClickListener() {
                        @Override
                        public void onClick(View v) {
//                           Toast.makeText(context, "点击了"+0, Toast.LENGTH_SHORT).show();
                            intent.putExtra("commodityId", bab.get(0).getCommodityId());
                            context.startActivity(intent);
                        }
                    });

                    viewHolder2.iv2.setOnClickListener(new View.OnClickListener() {
                        @Override
                        public void onClick(View v) {
                            intent.putExtra("commodityId", bab.get(1).getCommodityId());
                            context.startActivity(intent);
                        }
                    });

                    viewHolder2.iv3.setOnClickListener(new View.OnClickListener() {
                        @Override
                        public void onClick(View v) {
                            intent.putExtra("commodityId", bab.get(2).getCommodityId());
                            context.startActivity(intent);
                        }
                    });
                    viewHolder2.iv4.setOnClickListener(new View.OnClickListener() {
                        @Override
                        public void onClick(View v) {
//                           Toast.makeText(context, "点击了"+0, Toast.LENGTH_SHORT).show();
                            intent.putExtra("commodityId", bab.get(3).getCommodityId());
                            context.startActivity(intent);
                        }
                    });

                    viewHolder2.iv5.setOnClickListener(new View.OnClickListener() {
                        @Override
                        public void onClick(View v) {
                            intent.putExtra("commodityId", bab.get(4).getCommodityId());
                            context.startActivity(intent);
                        }
                    });


                }
                break;
            case 3:
                Log.e("getView: ", "=========热卖==3333=======");
                if (objects.get(position).equals(bla)) {

                    viewHolder3.tv1.setText(bla.get(0).getCommodityName() + "");
                    viewHolder3.tv11.setText(bla.get(0).getMinDiscount() + "");
                    viewHolder3.tv2.setText(bla.get(1).getCommodityName() + "");
                    viewHolder3.tv22.setText(bla.get(1).getMinDiscount() + "");
                    viewHolder3.tv3.setText(bla.get(2).getCommodityName() + "");
                    viewHolder3.tv33.setText(bla.get(2).getMinDiscount() + "");
                    Picasso.with(context).load(bla.get(0).getCommodityIcon()).placeholder(R.mipmap.ic_default_image).into(viewHolder3.iv1);
                    Picasso.with(context).load(bla.get(1).getCommodityIcon()).placeholder(R.mipmap.ic_default_image).into(viewHolder3.iv2);
                    Picasso.with(context).load(bla.get(2).getCommodityIcon()).placeholder(R.mipmap.ic_default_image).into(viewHolder3.iv3);

                    final Intent intent = new Intent(context, CommodityDtailsActivity.class);
                    viewHolder3.iv1.setOnClickListener(new View.OnClickListener() {
                        @Override
                        public void onClick(View v) {
//                           Toast.makeText(context, "点击了"+0, Toast.LENGTH_SHORT).show();
                            intent.putExtra("commodityId", mBean.getResult().getBestsellingArea().get(0).getCommodityId());
                            context.startActivity(intent);
                        }
                    });

                    viewHolder3.iv2.setOnClickListener(new View.OnClickListener() {
                        @Override
                        public void onClick(View v) {
                            intent.putExtra("commodityId", mBean.getResult().getBestsellingArea().get(1).getCommodityId());
                            context.startActivity(intent);
                        }
                    });

                    viewHolder3.iv3.setOnClickListener(new View.OnClickListener() {
                        @Override
                        public void onClick(View v) {
                            intent.putExtra("commodityId", mBean.getResult().getBestsellingArea().get(1).getCommodityId());
                            context.startActivity(intent);
                        }
                    });


                }
                break;
            case 4:
                Log.e("getView: ", "===========4444=======");
                ShopCommodityRemmendRecyclerAdapter adapter = new ShopCommodityRemmendRecyclerAdapter(mBean, context);
                GridLayoutManager manager = new GridLayoutManager(context, 2);
                viewHolder4.rv.setLayoutManager(manager);
//                    viewHolder4.rv.addItemDecoration(new RecyclerViewDivider(context,GridLayoutManager.HORIZONTAL));
//                    viewHolder4.rv.addItemDecoration(new RecyclerViewDivider(context,GridLayoutManager.VERTICAL));

                adapter.setOnItemClickListener(new ShopCommodityRemmendRecyclerAdapter.OnItemClickListener() {
                    @Override
                    public void onClick(int index) {
                        Intent intent = new Intent(context, CommodityDtailsActivity.class);
                        intent.putExtra("commodityId", mBean.getResult().getRecommendedArea().get(index).getCommodityId());
                        context.startActivity(intent);
                    }
                });
                viewHolder4.rv.setAdapter(adapter);
                break;
            case 5:
                Log.e("getView: ", "===========555=======");
                if (objects.get(position).equals(ma)) {
                    //轮播
                    for (int i = 0; i < mBean.getResult().getMiddleAds().size(); i++) {
                        DefaultSliderView defaultSliderView = new DefaultSliderView(context);
                        defaultSliderView.image(mBean.getResult().getMiddleAds().get(i).getAdPicture());
                        //轮播点击事件

                        final int finalI = i;
                        defaultSliderView.setOnSliderClickListener(new BaseSliderView.OnSliderClickListener() {
                            @Override
                            public void onSliderClick(BaseSliderView slider) {
                                Intent intent = new Intent(context, CommodityDtailsActivity.class);
                                intent.putExtra("commodityId", mBean.getResult().getMiddleAds().get(finalI).getCommodityId());
                                context.startActivity(intent);
                            }
                        });
                        viewHolder5.slider.addSlider(defaultSliderView);
                    }
                    viewHolder5.slider.setDuration(2000);
                }
                break;
        }

        return convertView;
    }

    class ViewHolder {
        RelativeLayout rl;
        RecyclerView rv;
    }

    class ViewHolder1 {
        RecyclerView rv;
    }

    class ViewHolder2 {
        TextView tv1, tv11, tv2, tv22, tv3, tv33, tv4, tv44, tv5, tv55;
        ImageView iv1, iv2, iv3, iv4, iv5;
    }

    class ViewHolder3 {
        TextView tv1, tv11, tv2, tv22, tv3, tv33;
        ImageView iv1, iv2, iv3;
    }

    class ViewHolder4 {
        RecyclerView rv;
    }

    class ViewHolder5 {
        SliderLayout slider;
    }


}
